package com.sd.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sd.coupon.entity.Collection;
import com.sd.coupon.model.req.CollectionPageReq;
import com.sd.coupon.service.ICollectionService;
import com.sd.coupon.service.MCollectionService;
import com.sd.coupon.util.Contant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class MCollectionServiceImpl implements MCollectionService {

    @Resource
    ICollectionService collectionService;

    @Override
    public Set<Long> getNumIidListByUserId(Long userId){
        QueryWrapper<Collection> queryWapper = new QueryWrapper<>();
        queryWapper.eq(Contant.Collection.USER_ID,userId);
        queryWapper.orderByDesc(Contant.Collection.sort);
        List<Collection> collectionItemIdList = collectionService.list( queryWapper);

        if( collectionItemIdList!=null && collectionItemIdList.size()>0){

            Set<Long> numIidList = new HashSet<>();
            for (Collection c : collectionItemIdList) {
                if(c!=null && c.getNumIid()!=null){
                    numIidList.add(c.getNumIid());
                }
            }
            return numIidList;
        }
        return null;
    }

    @Override
    public Boolean addOrDeleteNumIid(Collection collection){
        Collection collect = new Collection();
        collect.setNumIid(collection.getNumIid());
        if (collection.getUserId() == null) {
            collect.setUserId(1L);
        }else{
            collect.setUserId(collection.getUserId());
        }

        boolean res = true;
        if (collection.getIsDelete() != null) {
            collect.setIsDelete(collection.getIsDelete());
            UpdateWrapper<Collection> wrapper = new UpdateWrapper<Collection>();
            wrapper.set(Contant.Collection.USER_ID, collect.getUserId());
            wrapper.set(Contant.NUMIID, collect.getNumIid());
            res = collectionService.update(collect, wrapper);
        } else {
            res = collectionService.save(collect);
        }
        return res;
    }


}
